home *** CD-ROM | disk | FTP | other *** search
- Path: news.Stanford.EDU!usenet
- From: brien@leland.stanford.edu (brien oberstein)
- Newsgroups: comp.lang.c++
- Subject: Re: Copy constructing an already default constructed object
- Date: Fri, 26 Jan 1996 15:28:46 GMT
- Organization: Stanford University
- Message-ID: <3108ef14.340699@nntp>
- References: <4e906b$stk@elaine32.Stanford.EDU> <4eal0n$hgq@dawn.mmm.com>
- NNTP-Posting-Host: tip-mp20-ncs-4.stanford.edu
-
- On 26 Jan 1996 13:29:59 GMT, kjhopps@mmm.com (Kevin J Hopps) wrote:
-
- >brien oberstein (brien@leland.Stanford.EDU) wrote:
- >> I'd like to get some opinions on the best/cleanest way
- >> to accomplish the following:
- >
- >> I've got an object which has already been constructed
- >> via its default constructor which just sets all pointers
- >> to NULL. Whats the best way to deep-copy into it?
- >
- >[ example and discussion arriving at the conclusion that
- > overloaded operator=() is required ]
- >
- >> //
- >> // deep-copy =
- >> //
- >> A& A::operator =(const A& other)
- >> {
- >> A empty;
- >> A tmp(other);
- >> memcpy(this, &tmp, sizeof(A));
- >> memcpy(&tmp, &empty, sizeof(A));
- >> return *this;
- >> }
- >
- >
- >> I'd like to know what people think of the solution I've reached.
- >> I figure that this type of shit is common enough so there should
- >> be some widely accepted solution to this problem. Or maybe its
- >> not, but believe me that the situation does occur.
- >
- >You have arrived at the correct conclusion -- that operator=() is
- >what you are looking for. However, the function you've coded has
- >some problems.
- >
- >First, you have to decide for yourself what the copy semantics of
- >your class are -- deep or shallow. Regardless of what you decide,
- >I think that the copy constructor and the assignment operator
- >should produce identical results. Frequently one is written in
- >terms of the other. Ultimately they must do an exhaustive copy
- >of each data member. Using memcpy as above can overwrite
- >compiler-generated data within the object and can have unpredictable
- >results.
- <snip>
-
- What compiler generated data? The vtable pointer? What if I'm not
- using virtual functions?
-
-
- Ok. Lemme give a better example of the actual problem I'm having.
- The object I have has other objects within it (which allocate
- dynamically), but no pointers are contained _directly_ in its class.
-
- class A{
- public:
- A();
- A(const A&);
- A(char *);
-
- private:
- B b0, b1;
- };
-
- class B{
- public:
- B();
- B(const B&);
- B(char *);
-
- private:
- char *p;
- }
-
- So my copy constructor for A is easy:
- A::A(const A& other) : b0(other.b0), b1(other.b1) { }
-
- Now how do you do the copy? Do you have to
- overload = for all the contained operators too?
- ie,
-
- A::operator =(const A& other)
- {
- b0 = other.b0 ;
- b1 = other.b1;
- }
-
- To me this seems like a major pain in the butt. All I really
- want to do is have the copy constructor invoked on the piece that
- piece of memory. Why can't this be done?
-
- -brien
-